package com.delio.client.produccion.notaingresos;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.delio.client.idioma.DelioConstants;
import com.delio.client.produccion.articulos.Articulos;
import com.delio.client.produccion.detallenotaingresos.DetalleNotaIngreso;
import com.delio.client.produccion.detallenotaingresos.DetalleNotaIngresos;
import com.delio.client.produccion.detallepedidos.DetallePedidos;
import com.delio.client.produccion.estadonotaingresos.EstadoNotaIngresos;
import com.delio.client.produccion.motivos.Motivos;
import com.delio.client.produccion.pedidos.SearchPedidoNIForm;
import com.delio.client.produccion.tipodocumentos.TipoDocumentos;
import com.delio.client.util.ComboboxFieldBinding;
import com.delio.client.util.Utilities;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.binding.FieldBinding;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.GridEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MenuEvent;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.button.ButtonBar;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.HiddenField;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.filters.GridFilters;
import com.extjs.gxt.ui.client.widget.grid.filters.StringFilter;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.menu.Menu;
import com.extjs.gxt.ui.client.widget.menu.MenuItem;
import com.extjs.gxt.ui.client.widget.toolbar.PagingToolBar;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;

public class NotaIngresoForm extends ContentPanel{
	private DelioConstants x = GWT.create(DelioConstants.class);
	public Button btnDeleteDetalleNotaIngreso,btnGuardar,btnDeleteNotaIngreso;
	ToolBar toolBar,toolBar1;
	ContentPanel cpw,ctp,cpi,cpg,cpf,cpe;
	BorderLayoutData sup,inf,east,center,nor,c,cen,cg;
	public TextField<String> numDocumento,detpedido,loteproveedorf;
	ComboBox<ModelData> enotaingreso,tipdocumento;
	ComboBox<BeanModel> motivo;
	HiddenField<String> id,idf;
	Integer idmotivo,idestadonotaingreso,idtipdocumento,idartf,idni;
	String codartf;
	private FormBinding formbinding,formbinding1;
	DateField fechaf;
	FormPanel fp,f;
	private BeanModel selectedModel;
	ModelData selectedModel1;
	NumberField cantidadf;
	ComboBox<ModelData> ednotaingresof,articulof;
	Grid<BeanModel> grid;
	Grid<ModelData> grid1;
	Menu contMenuDP; 
	MenuItem miDP;
	
	public NotaIngresoForm(){
		setHeaderVisible(false);
		BorderLayout layout = new BorderLayout();
		setLayout(layout);
		Motivos.getStore();
		Articulos.getStore();
		EstadoNotaIngresos.listarestadonotaingresos();
		TipoDocumentos.getStore();
		NotaIngresos.getPaginate();
		DetalleNotaIngresos.getPaginate();
       	formulario();
       	Form();
     	SearchPedidoNIForm.nif_concatPDP = NotaIngresoForm.this;
	    cpw = new ContentPanel();
	    cpw.setHeading("Editar Nota Ingreso");
	    cpw.setIconStyle("images/icons/people.png");
	    sup = new BorderLayoutData(LayoutRegion.NORTH);
	    sup.setMargins(new Margins(0,3,3,3));
	    
        inf = new BorderLayoutData(LayoutRegion.SOUTH);
        inf.setMargins(new Margins(0,3,0,0));

	    cpw.add(fp,sup);
	    cpw.setLayout(new FitLayout());
	    	   	    
	    cpe = new ContentPanel();
	    cpe.setHeaderVisible(false);
	    cpe.setLayout(new BorderLayout());
	       
	    cpf = new ContentPanel();
	    cpf.setHeading("Editar Detalle Nota Ingreso");
	    cpf.setIconStyle("images/icons/people.png"); 
	    cpf.add(f,inf);
	    cpf.setLayout(new FitLayout());
	   
        east = new BorderLayoutData(LayoutRegion.EAST,350);
	    east.setMargins(new Margins(0));
	    east.setSplit(true);
	    east.setCollapsible(true);
	    
	    center  = new BorderLayoutData(LayoutRegion.CENTER);
	    center.setMargins(new Margins(0));
	    center.setCollapsible(true);
	    
	    ctp = new ContentPanel();
	    ctp.setHeaderVisible(false);
	    ctp.setLayout(new BorderLayout());
		 	    
		nor  = new BorderLayoutData(LayoutRegion.NORTH,300);
		nor.setMargins(new Margins(0,3,3,3));
		 
		cpi = new ContentPanel();
		cpi.setHeading("Listado de Notas de Ingresos");
		cpi.setIconStyle("images/icons/table.png");
		cpi.setLayout(new BorderLayout());
		  
		c = new BorderLayoutData(LayoutRegion.CENTER);
		 		    
		cen   = new BorderLayoutData(LayoutRegion.CENTER);
		cen.setMargins(new Margins(3));
		    
		cpg = new ContentPanel();
		cpg.setHeading("Listado de Detalle de Notas de Ingreso");
		cpg.setIconStyle("images/icons/table.png");
		cpg.setLayout(new BorderLayout());
			
	 	cg = new BorderLayoutData(LayoutRegion.CENTER,100);
		    
	    final PagingToolBar toolBarPag = new PagingToolBar(10);
        toolBarPag.bind(NotaIngresos.loader);
        
        List<ColumnConfig> conf = new ArrayList<ColumnConfig>();
        CheckBoxSelectionModel<BeanModel> sm = new CheckBoxSelectionModel<BeanModel>();
    
        conf.add(sm.getColumn()); 
        ColumnConfig col  = new ColumnConfig("motdescripcion",x.motivo(),100);
	    conf.add(col);
	    col  = new ColumnConfig("tipdocdescripcion",x.tipodocumento(),100);
	    conf.add(col);
	    col  = new ColumnConfig("numerodocumento",x.numerodocumento(),100);
	    conf.add(col);
	    col  = new ColumnConfig("estnotingdescripcion",x.fecha(),100);
	    conf.add(col);
	    
	    final ColumnModel cm = new ColumnModel(conf);
	    
	    grid = new Grid<BeanModel>(NotaIngresos.store, cm);  
        grid.addListener(Events.Attach, new Listener<GridEvent<BeanModel>>() {  
          public void handleEvent(GridEvent<BeanModel> be) {  
        	  NotaIngresos.loader.load(0, 10);  
          }  
        }); 
        sm.setSelectionMode(SelectionMode.SINGLE);
        grid.setSelectionModel(sm);
        grid.getSelectionModel().addListener(Events.SelectionChange,
           new Listener<SelectionChangedEvent<BeanModel>>() {
			@Override
			public void handleEvent(SelectionChangedEvent<BeanModel> be) {		
    		   if(be.getSelection().size() == 1) {
	             formbinding.bind((BeanModel) be.getSelection().get(0));
	             selectedModel = be.getSelectedItem();
	             //DetalleNotaIngresos.loader.load();
	             //if(iddireccioncliente!=null)
	             //  direccion.setRawValue(selectedModel.get("direccion").toString());
                 DetalleNotaIngresos.notaingreso_id = Integer.parseInt(selectedModel.get("id").toString());
	             DetalleNotaIngresos.loader.load();
	             btnDeleteNotaIngreso.setEnabled(true); 
	           }
    		   if(be.getSelection().size()==0){
    			 DetalleNotaIngresos.store.removeAll();
    			 btnDeleteNotaIngreso.setEnabled(false);
    	       //clearForm();
                 formbinding.unbind();
               }
    			if(be.getSelection().size()>1){
       			 btnDeleteNotaIngreso.setEnabled(false);
    			 formbinding.unbind();
    		   }
			}
          });
        
        grid.setLoadMask(true);  
        grid.setBorders(true);       
        GridFilters filters = new GridFilters();
        StringFilter motivoFilter = new StringFilter("motdescripcion");        
        StringFilter tipdocumentoFilter = new StringFilter("tipdocdescripcion");
        StringFilter nrodocumentoFilter = new StringFilter("numerodocumento");
        StringFilter detallepedidoFilter = new StringFilter("estnotingdescripcion");
          
        filters.addFilter(motivoFilter);        
        filters.addFilter(tipdocumentoFilter);        
        filters.addFilter(nrodocumentoFilter);        
        filters.addFilter(detallepedidoFilter);
          
        grid.addPlugin(filters);                
        grid.addPlugin(sm);
        formbinding = new FormBinding(fp);
        formbinding.setStore(NotaIngresos.store);
        formbinding.addFieldBinding(new FieldBinding(id,"id"));
        formbinding.addFieldBinding(new ComboboxFieldBinding(motivo,"motdescripcion"));
        formbinding.addFieldBinding(new ComboboxFieldBinding(tipdocumento,"tipdocdescripcion"));
        formbinding.addFieldBinding(new FieldBinding(numDocumento,"numerodocumento"));
        formbinding.addFieldBinding(new FieldBinding(detpedido,"detallepedido_id"));
        formbinding.addFieldBinding(new ComboboxFieldBinding(enotaingreso,"estnotingdescripcion"));
		   
	    final PagingToolBar toolBarPag1 = new PagingToolBar(10);
        toolBarPag1.bind(DetalleNotaIngresos.loader);
        
        List<ColumnConfig> conf1 = new ArrayList<ColumnConfig>();
        CheckBoxSelectionModel<ModelData> sm1 = new CheckBoxSelectionModel<ModelData>();
    
        conf1.add(sm1.getColumn()); 
        ColumnConfig col1  = new ColumnConfig("fecha","Fecha",100);
	    conf1.add(col1);
	    col1  = new ColumnConfig("artdescripcion","Articulo",100);
	    conf1.add(col1);
	    col1  = new ColumnConfig("loteproveedor","Lote Proveedor",100);
	    conf1.add(col1);
	    col1  = new ColumnConfig("cantidad","Cantidad",100);
	    conf1.add(col1);
	   	    
	    final ColumnModel cm1 = new ColumnModel(conf1);
	    grid1 = new Grid<ModelData>(DetalleNotaIngresos.store, cm1);  
        grid1.addListener(Events.Attach, new Listener<GridEvent<BeanModel>>() {  
          public void handleEvent(GridEvent<BeanModel> be) {  
        	  DetalleNotaIngresos.loader.load(0, 10);  
          }  
        }); 
        sm1.setSelectionMode(SelectionMode.SINGLE);
        grid1.setSelectionModel(sm1);
        grid1.getSelectionModel().addListener(Events.SelectionChange,
           new Listener<SelectionChangedEvent<ModelData>>() {
			@Override
			public void handleEvent(SelectionChangedEvent<ModelData> be) {		
			   if(be.getSelection().size()==0){
				   clearForm1();
                   formbinding1.unbind();
				   btnDeleteDetalleNotaIngreso.setEnabled(false);
				   //DetalleNotaIngresos.store.removeAll();
				   
				 }
			   if(be.getSelection().size()==1){
				   formbinding1.bind((ModelData) be.getSelection().get(0));
	  	          // selectedModel1 = be.getSelectedItem();
	  	            
				   btnDeleteDetalleNotaIngreso.setEnabled(true);
				   selectedModel1 = be.getSelectedItem();
		           DetalleNotaIngresos.detallenotaingreso_id = selectedModel1.get("id");
		           System.out.println("detallenotaingreso_id"+DetalleNotaIngresos.detallenotaingreso_id);
		          }
    		   if(be.getSelection().size()>1)
    			   btnDeleteDetalleNotaIngreso.setEnabled(false);
    			   formbinding1.unbind();
			}
          });
        
        grid1.setLoadMask(true);  
        grid1.setBorders(true);       
        GridFilters filt     = new GridFilters();
        StringFilter fecha   = new StringFilter("fecha");        
        StringFilter art     = new StringFilter("articulo");        
        StringFilter lteprov = new StringFilter("loteproveedor");
        StringFilter cant    = new StringFilter("cantidad");

          
        filt.addFilter(fecha);        
        filt.addFilter(art);        
        filt.addFilter(lteprov);               
        filt.addFilter(cant);
         
        grid1.addPlugin(filt);                
        grid1.addPlugin(sm1);
        formbinding1 = new FormBinding(f);
        formbinding1.setStore(DetalleNotaIngresos.store);
        formbinding1.addFieldBinding(new FieldBinding(idf,"id"));
        formbinding1.addFieldBinding(new FieldBinding(fechaf,"fecha"));
        formbinding1.addFieldBinding(new FieldBinding(id,"notaingreso_id"));
        formbinding1.addFieldBinding(new ComboboxFieldBinding(articulof,"artdescripcion"));
	    formbinding1.addFieldBinding(new FieldBinding(loteproveedorf,"loteproveedor"));
	    formbinding1.addFieldBinding(new FieldBinding(cantidadf,"cantidad"));
     
	    contMenuDP = new Menu();  
	    miDP = new MenuItem();  
	    miDP.setText("Buscar DPedido");  
	    miDP.setIconStyle("images/icons/search_lupa.png");  
	    miDP.addSelectionListener(new SelectionListener<MenuEvent>() {  
	    public void componentSelected(MenuEvent ce) {  
	      SearchPedidoNIForm spf = new SearchPedidoNIForm();
	      spf.show();
	       }  
	    });  
	    contMenuDP.add(miDP);  
	    detpedido.setContextMenu(contMenuDP); 
	    
        cpi.setTopComponent(toolbar());
        cpi.add(grid,c);
		cpi.setBottomComponent(toolBarPag);		
   
		cpg.setTopComponent(toolbar1());
		cpg.add(grid1,cg);
	    cpg.setBottomComponent(toolBarPag1);
	
		ctp.add(cpi,nor);
		ctp.add(cpg,cen);
		
		cpe.add(cpw,nor);
		cpe.add(cpf,cen);
		
        add(ctp,center);
	    add(cpe,east);
	    
	}
   
    
   public ToolBar toolbar(){
	  toolBar = new ToolBar(); 
      btnDeleteNotaIngreso = new Button("Eliminar", IconHelper.createPath("images/icons/delete.gif"));
      btnDeleteNotaIngreso.setEnabled(false);
      toolBar.add(btnDeleteNotaIngreso);
      btnDeleteNotaIngreso.addSelectionListener(new SelectionListener<ButtonEvent>(){
   	  public void componentSelected(ButtonEvent ce){
	    if(id.getValue() != null){	 
	      Listener<MessageBoxEvent> callback = new Listener<MessageBoxEvent>() {	 
		   public void handleEvent(MessageBoxEvent be) {
            if(be.getButtonClicked().getItemId().equals(Dialog.YES)) { 
		        NotaIngresos.eliminar(Integer.parseInt(id.getValue()));
			    NotaIngresos.loader.load();
	         }
            }
	      };
	       String msg = "Esta seguro que desea eliminar la Nota de Ingreso?";
	       MessageBox.confirm(x.eliminarCliente(), msg, callback);
	 	  }else{
		   MessageBox.alert(x.seleccionar(), "Seleccione una fila porfavor", null);
	      }
		 }
	  });
     return toolBar;
    }
   
	  public ToolBar toolbar1(){
		toolBar1 = new ToolBar();
		btnDeleteDetalleNotaIngreso = new Button("Eliminar",IconHelper.createPath("images/icons/delete.gif"));
		btnDeleteDetalleNotaIngreso.setEnabled(false);
		toolBar1.add(btnDeleteDetalleNotaIngreso);
		btnDeleteDetalleNotaIngreso.addSelectionListener(new SelectionListener<ButtonEvent>(){
		  public void componentSelected(ButtonEvent ce){
			    Listener<MessageBoxEvent> callback = new Listener<MessageBoxEvent>() {	 
				  public void handleEvent(MessageBoxEvent be) {
		           if (be.getButtonClicked().getItemId().equals(Dialog.YES)) { 
				        DetalleNotaIngresos.eliminar(DetalleNotaIngresos.detallenotaingreso_id);
					    DetalleNotaIngresos.loader.load();
			         }
		           }
			     };
			     String msg = "Esta seguro que desea eliminar esta direccion del Cliente?";
			     MessageBox.confirm("Eliminar Direccion Cliente?", msg, callback);
			 }
		   });
	   return toolBar1;
	 }
	
	public ButtonBar buttonForm(){
	  ButtonBar bb = new ButtonBar();
	  btnGuardar = new Button("Guardar",IconHelper.createPath("images/icons/save_all.png"));		  
	  btnGuardar.addSelectionListener(new SelectionListener<ButtonEvent>(){
		public void componentSelected(ButtonEvent ve){
			
	     if(validarForm()&& validarIdsForm()){  
	        final NotaIngreso ni = new NotaIngreso();
	         ni.setTipodocumento_id(idtipdocumento);
	         ni.setMotivo_id(idmotivo);
	         ni.setValesalida_id(null);
	         ni.setEstadonotaingreso_id(idestadonotaingreso);
	         ni.setDetallepedido_id(DetallePedidos.detallepedido_id);
	         ni.setNumerodocumento(numDocumento.getValue());
		 if(id.getValue() == null){	
		    NotaIngresos.guardar(ni);
	        NotaIngresos.loader.load();
	        clearForm();
	      }
	     if(id.getValue() != null){
		 Listener<MessageBoxEvent> callback = new Listener<MessageBoxEvent>() {	 
		  public void handleEvent(MessageBoxEvent be) {
		    if(be.getButtonClicked().getItemId().equals(Dialog.YES)) {  
		       ni.setId(Integer.parseInt(id.getValue()));
		       NotaIngresos.actualizar(ni);
		       NotaIngresos.loader.load();
			  }
			}
	      };
	      String msg = "Esta seguro que desea Actualizar la Nota de Ingreso?";
	      MessageBox.confirm("Actualizar Nota de Ingreso?", msg, callback);
	 	  }
        }
	   }
      });
	  HorizontalAlignment align =  HorizontalAlignment.CENTER;
	  bb.add(btnGuardar);
	  bb.setAlignment(align);
	  return bb;
	  }
	 
   public void formulario(){
    	fp = new FormPanel();
		fp.setHeaderVisible(false);
		fp.setFrame(true);
		id = new HiddenField<String>();
		id.setVisible(false);
		Motivos.loaderCombo.load();
		motivo = new ComboBox<BeanModel>();  
		motivo.setEmptyText("Select a state...");  
		motivo.setAllowBlank(false);
		motivo.setDisplayField("descripcion");  
		motivo.setFieldLabel(x.motivo());
		motivo.setWidth(150);  
		motivo.setStore(Motivos.storeCombo);
		motivo.setValueField("descripcion");
		motivo.setTypeAhead(true);  
		motivo.setTriggerAction(TriggerAction.ALL); 
		motivo.addSelectionChangedListener(new SelectionChangedListener<BeanModel>(){
		  public void selectionChanged(SelectionChangedEvent<BeanModel> se) {
		    if(motivo.getValue()!=null){ 
               System.out.println("seleccionado...");
        	   idmotivo = Integer.parseInt(motivo.getValue().get("id").toString());
               TipoDocumentos.tipodocumento_id = idmotivo;
              }
     		}
		});
		TipoDocumentos.loaderCombo.load();
  		tipdocumento = new ComboBox<ModelData>();  
		tipdocumento.setEmptyText("Select a state...");  
		tipdocumento.setAllowBlank(true);
		tipdocumento.setDisplayField("descripcion");  
		tipdocumento.setFieldLabel(x.tipodocumento());
		tipdocumento.setWidth(150);  
		tipdocumento.setStore(TipoDocumentos.storeCombo);
		tipdocumento.setValueField("descripcion");
		tipdocumento.setTypeAhead(true);  
		tipdocumento.setTriggerAction(TriggerAction.ALL); 
		tipdocumento.addSelectionChangedListener(new SelectionChangedListener<ModelData>(){
		  public void selectionChanged(SelectionChangedEvent<ModelData> se) {
     	    if(tipdocumento.getValue()!=null){ 
     		  idtipdocumento = Integer.parseInt(tipdocumento.getValue().get("id").toString());
			 }
    	  }
		});	
		numDocumento = new TextField<String>();
		numDocumento.setFieldLabel(x.numerodocumento());
		
	    detpedido = new TextField<String>();
	    detpedido.setFieldLabel(x.detallepedido());
		detpedido.setAllowBlank(false);
	    
		EstadoNotaIngresos.loader.load();
		enotaingreso =  new ComboBox<ModelData>();
		enotaingreso.setEditable(false);
		enotaingreso.setFieldLabel(x.estado());
		enotaingreso.setDisplayField("descripcion");  
		enotaingreso.setEmptyText("Select a state...");  
		enotaingreso.setWidth(150);  
		enotaingreso.setStore(EstadoNotaIngresos.store);
		enotaingreso.setTypeAhead(true);  
		enotaingreso.setTriggerAction(TriggerAction.ALL); 
		enotaingreso.setValueField("descripcion");
		enotaingreso.setAllowBlank(false);
		enotaingreso.setUseQueryCache(false);
		enotaingreso.addSelectionChangedListener(new SelectionChangedListener<ModelData>(){
		  public void selectionChanged(SelectionChangedEvent<ModelData> se) {
			if(enotaingreso.getValue()!=null){   
     		  idestadonotaingreso = Integer.parseInt(enotaingreso.getValue().get("id").toString());
     		  }
			 }
		});	
		fp.add(id);	
		fp.add(motivo);
		fp.add(tipdocumento);
		fp.add(numDocumento);
		fp.add(detpedido);
		fp.add(enotaingreso);
		fp.add(buttonForm());
		Utilities.setLengh(numDocumento, detpedido,20);
	}
	
   public void clearForm(){
	 fp.clear();
    }
   
   public boolean validarForm(){
     return fp.isValid();
    }
   
   public boolean validarIdsForm(){
	  boolean var = false;
	  Integer idmot = idmotivo;
	  Integer idtd  = idtipdocumento;
	  Integer ideni = idestadonotaingreso;
	  Integer iddp  = DetallePedidos.detallepedido_id;
	  if(idmot!=null && idtd!=null && ideni!=null && iddp != null)
		  var = true;
	 return var;
   }
   
   public void clearForm1(){
	   f.clear();
   }
   
   public boolean validarForm1(){
	   return f.isValid();
   }
  
   public boolean validarIdsForm1(){
	boolean var = false; 
	//idni = Integer.parseInt(id.getValue().toString());
	
	if(idartf!=null && id.getValue()!=null)
	       var=true;
	return var;
   }
   
   public ButtonBar buttonForm1(){
    ButtonBar bb = new ButtonBar();
    btnGuardar = new Button("Guardar",IconHelper.createPath("images/icons/save_all.png"));		  
    btnGuardar.addSelectionListener(new SelectionListener<ButtonEvent>(){
	public void componentSelected(ButtonEvent ve){
     if(validarForm1()&&validarIdsForm1()){  
	   final DetalleNotaIngreso dni = new DetalleNotaIngreso();
	   dni.setNotaingreso_id(Integer.parseInt(id.getValue()));
	 /*dni.setMaquinaria_id(null);
	   dni.setCodigocolor_id(null);
	   dni.setEmpleado_id(null);
	   dni.setNumerorollo(null);*/
	   dni.setFecha(fechaf.getValue());
	   dni.setArticulo_id(Integer.parseInt(articulof.getValue().get("id").toString()));
	   dni.setLoteproveedor(loteproveedorf.getValue());
	 /*dni.setAgujasrotas(null);
	   dni.setMetroshilo(null);*/
	   dni.setCantidad(Double.parseDouble(cantidadf.getValue().toString()));
  
	 if(idf.getValue() == null){	
	   System.out.println("entro en guardar detallenotaingreso");
       DetalleNotaIngresos.guardar(dni);
       DetalleNotaIngresos.loader.load();
       clearForm1();
     }
     if(idf.getValue() != null){
	  Listener<MessageBoxEvent> callback = new Listener<MessageBoxEvent>() {	 
	  public void handleEvent(MessageBoxEvent be) {
	    if(be.getButtonClicked().getItemId().equals(Dialog.YES)) {  
	       dni.setId(Integer.parseInt(idf.getValue()));
	       DetalleNotaIngresos.actualizar(dni);
		   DetalleNotaIngresos.loader.load();
		  }
		}
      };
      String msg = "Esta seguro que desea Actualizar el Detalle Nota Ingreso?";
      MessageBox.confirm("Actualizar Detalle Nota Ingreso?", msg, callback);
 	    }
       }
      }
     });

     HorizontalAlignment align =  HorizontalAlignment.CENTER;
     bb.add(btnGuardar);
     bb.setAlignment(align);
    return bb;
  }
   
    public void Form(){
      f = new FormPanel();
	  f.setHeaderVisible(false);
	  f.setFrame(true);
	  idf = new HiddenField<String>();
	  idf.setVisible(false);
	  fechaf = new DateField();
	  fechaf.setName("fecha");
	  fechaf.getPropertyEditor().setFormat(DateTimeFormat.getFormat("dd/MM/yyyy"));
	  fechaf.setAllowBlank(false);
	  fechaf.setFieldLabel(x.fecha());
	  fechaf.setValue(new Date());
	  articulof = new ComboBox<ModelData>();
	  articulof.setEmptyText("Select a state...");  
	  articulof.setAllowBlank(false);
	  articulof.setDisplayField("descripcion");  
	  articulof.setFieldLabel(x.articulos());
	  articulof.setWidth(150);  
	  articulof.setStore(Articulos.storeCombo);
	  articulof.setValueField("descripcion");
	  articulof.setTypeAhead(true);  
	  articulof.setTriggerAction(TriggerAction.ALL); 
	  articulof.addSelectionChangedListener(new SelectionChangedListener<ModelData>(){
	    public void selectionChanged(SelectionChangedEvent<ModelData> se) {
	     if(articulof.getValue()!=null){	
 		  idartf  = Integer.parseInt(articulof.getValue().get("id").toString());
 		  }
         }
	  });	

      loteproveedorf = new TextField<String>();
	  loteproveedorf.setFieldLabel(x.loteproveedor());
	  
	  cantidadf = new NumberField();
      cantidadf.setFieldLabel(x.cantidad());

      f.add(idf);
      f.add(fechaf);
      f.add(articulof,new FormData("-20"));
      f.add(loteproveedorf,new FormData("-20"));
	  f.add(cantidadf,new FormData("-20"));
	  f.add(buttonForm1(),new FormData("-20"));
   }
      
}